<template>
	<view>
		<view class="c-wrap">
			<view class="cell-item">
				<view class="left">头像</view>
				<view class="right">
					<q-upload :isAvatar="true" :avatarSrc='userInfo.avatar' :maxCount="1" :file="coverMediaList"
						ref="ossUpload" @success="uploadSuccess" :autoUpload="true"></q-upload>
				</view>
			</view>
			<view class="cell-item">
				<view class="left">昵称</view>
				<view @click="jump(userInfo.nickname, 'nickname')" class="right">
					<text>{{userInfo.nickname}}</text>
					<u-icon name="arrow-right"></u-icon>
				</view>
			</view>
			<view class="cell-item">
				<view class="left">性别</view>
				<view class="right" @click="onOpenGender">
					<text>{{getGenderName(userInfo.gender) }}</text>
					<u-icon name="arrow-right"></u-icon>
				</view>
			</view>
			<!-- 	<view class="cell-item">
				<view class="left">地区</view>
				<view class="right" @click="showRegion = true">
					<text>{{userInfo.province}} {{userInfo.city}}</text>
					<u-icon name="arrow-right"></u-icon>
				</view>
			</view> -->
			<view class="cell-item">
				<view class="left">生日</view>
				<view class="right" @click="onOpenBirthday">
					<text>{{userInfo.birthday}}</text>
					<u-icon name="arrow-right"></u-icon>
				</view>
			</view>
			<view class="cell-item">
				<view class="left">个性签名</view>
				<view @click="jump(userInfo.intro, 'intro')" class="right">
					<text>{{userInfo.intro || '这个人很懒，没留下什么'}}</text>
					<u-icon name="arrow-right"></u-icon>
				</view>
			</view>
		</view>
		<!-- 性别选择器 -->
		<u-action-sheet :show="showGender" :actions="genderActions" title="请选择性别" @close="showGender = false"
			@select="saveGender">
		</u-action-sheet>
		<!-- 地区选择 -->
		<u-picker :show="showRegion" mode="region" @confirm="saveRegion" @cancel="showRegion = false"></u-picker>
		<!-- 生日选择器 -->
		<u-datetime-picker :show="showBirthday" :minDate="631123200000" :maxDate="1262275200000" mode="date"
			@confirm="saveBirthday" @cancel="showBirthday = false">
		</u-datetime-picker>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				form: {},
				showGender: false,
				showRegion: false,
				showBirthday: false,
				genderActions: [{
						value: 0,
						name: '保密',
					},
					{
						value: 1,
						name: '男生',
					},
					{
						value: 2,
						name: '女生',
					}
				],
				coverMediaList: []
			};
		},
		computed: {
			userInfo() {
				return this.$store.state.user.userInfo
			}
		},
		onShow() {
			this.getUserInfo();
		},
		methods: {
			getGenderName(value) {
				// 遍历数组查找匹配的对象
				const gender = this.genderActions.find(item => item.value === value);
				// 如果找到则返回name，否则返回默认值
				return gender ? gender.name : '未知性别';
			},
			onOpenBirthday() {
				this.showBirthday = true;
			},
			onOpenGender() {
				this.showGender = true;
			},
			getUserInfo() {
				this.$H.post('user/getInfo').then(res => {
					this.$store.commit("set_info", res.result);
				});
			},
			// 编辑用户信息
			editUserInfo(field, value) {
				this.$H.post('user/setInfo', {
					field,
					value
				}).then(res => {
					if (res.code == 200) {
						this.getUserInfo();
					}
				});
			},
			uploadSuccess(url) {
				this.editUserInfo('avatar', url);
			},
			// 生日修改
			saveBirthday(e) {
				let date = uni.$u.timeFormat(e.value, 'yyyy-mm-dd');
				this.editUserInfo('birthday', date);
				this.showBirthday = false;

			},
			// 地区修改
			saveRegion(obj) {
				let province = obj.province.label;
				let city = obj.city.label;

				let form = {
					province: province,
					city: city
				};

				this.$H.post('user/userInfoEdit', form).then(res => {
					if (res.code == 200) {
						this.$store.state.user.userInfo.province = province;
						this.$store.state.user.userInfo.city = city;
					}
				});
			},
			// 修改性别
			saveGender(e) {
				this.editUserInfo('gender', e.value);

				this.showGender = false;
			},
			jump(value, field) {
				uni.navigateTo({
					url: 'submit?value=' + JSON.stringify(value) + '&field=' + field
				});
			}
		}
	};
</script>
<style lang="scss" scoped>
	.c-wrap {
		padding: 20rpx;
		background-color: #ffffff;
	}

	.bind-mobile {
		display: flex;
	}

	.cell-item {
		display: flex;
		align-items: center;
		padding: 30rpx;
		border-bottom: 1px solid #F5F5F5;

		.left {
			margin-right: 20rpx;
			white-space: nowrap;
		}

		.right {
			margin-left: auto;
			color: #616161;
			display: flex;
			align-items: center;

			text {
				margin-right: 10rpx;
			}
		}
	}
</style>